package alp.starcode.edms.dict.controller;

import alp.starcode.edms.dict.model.dto.DictTypeDto;
import alp.starcode.edms.dict.model.vo.DictTypeVo;
import alp.starcode.common.mybatis.page.Pagination;
import alp.starcode.common.web.result.Result;
import alp.starcode.edms.dict.service.DictTypeService;
import alp.starcode.edms.framework.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 字典类型控制器
 *
 * @author Clxini
 * @since 2025-01-17
 */
@Slf4j
@RestController
@RequestMapping("/dict/type")
@Api(tags = "字典模块-字典类型管理")
@Validated
public class DictTypeController extends BaseController {

    @Autowired
    private DictTypeService dictTypeService;

    @PostMapping("addDictType.do")
    @ApiOperation("新增字典类型")
    public Result<Void> addDictType(@Valid @RequestBody DictTypeDto dictTypeDto) {
        dictTypeService.addDictType(dictTypeDto);
        return Result.success();
    }

    @PostMapping("updateDictType.do")
    @ApiOperation("修改字典类型")
    public Result<Void> updateDictType(@Valid @RequestBody DictTypeDto dictTypeDto) {
        dictTypeService.updateDictType(dictTypeDto);
        return Result.success();
    }

    @PostMapping("deleteDictType.do")
    @ApiOperation("删除字典类型")
    public Result<Void> deleteDictType(
            @ApiParam(value = "字典类型ID", required = true)
            @RequestParam @NotBlank(message = "字典类型ID不能为空") String dictTypeId) {
        
        dictTypeService.deleteDictType(dictTypeId);
        return Result.success();
    }

    @GetMapping("getDictTypeById.do")
    @ApiOperation("根据ID查询字典类型")
    public Result<DictTypeVo> getDictTypeById(
            @ApiParam(value = "字典类型ID", required = true)
            @RequestParam @NotBlank(message = "字典类型ID不能为空") String dictTypeId) {
        
        return Result.success(dictTypeService.getDictTypeById(dictTypeId));
    }

    @GetMapping("getDictTypeByCode.do")
    @ApiOperation("根据编码查询字典类型")
    public Result<DictTypeVo> getDictTypeByCode(
            @ApiParam(value = "字典类型编码", required = true)
            @RequestParam @NotBlank(message = "字典类型编码不能为空") String dictTypeCode) {
        
        return Result.success(dictTypeService.getDictTypeByCode(dictTypeCode));
    }

    @GetMapping("pageDictType.do")
    @ApiOperation("分页查询字典类型")
    public Result<Pagination<DictTypeVo>> pageDictType(
            @ApiParam(value = "字典类型名称") @RequestParam(required = false) String dictTypeName,
            @ApiParam(value = "字典类型编码") @RequestParam(required = false) String dictTypeCode,
            Pagination<DictTypeVo> pagination) {
        
        return Result.success(dictTypeService.pageDictType(dictTypeName, dictTypeCode, pagination));
    }

    @GetMapping("getAllDictTypeList.do")
    @ApiOperation("查询所有字典类型")
    public Result<List<DictTypeVo>> getAllDictTypeList() {
        return Result.success(dictTypeService.getAllDictTypeList());
    }

    @GetMapping("checkDictTypeCodeExists.do")
    @ApiOperation("检查字典类型编码是否存在")
    public Result<Boolean> checkDictTypeCodeExists(
            @ApiParam(value = "字典类型编码", required = true) @RequestParam @NotBlank(message = "字典类型编码不能为空") String dictTypeCode,
            @ApiParam(value = "排除的ID") @RequestParam(required = false) String excludeId) {
        
        return Result.success(dictTypeService.checkDictTypeCodeExists(dictTypeCode, excludeId));
    }
} 